/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;

/**
* Used by log to maintain a new ID per thread
*/
public class Settings {
	private SettingsStore store=null;
	private static Settings settings = null;
	
	final static SimpleDateFormat date_format=new SimpleDateFormat("d MMM yyyy");
	final static SimpleDateFormat time_format=new SimpleDateFormat("k:mm:ss");
	final static SimpleDateFormat date_time_format=new SimpleDateFormat("d MMM yyyy k:mm:ss");

	public Settings(SettingsStore store) {
		this.store = store;
	}
	
	public SettingsStore getStore() {return store;}
	
	private static void init(){
		if (settings==null){
			settings=new Settings(new SettingsStorePropertiesFile("application.config"));
		}
	}
	
	public static void setSettingsStore(SettingsStore p_store){
		settings = new Settings(p_store);
	}

	public static SettingsStore getSettingsStore(){
		init();
		return settings.getStore();
	}

	public static Enumeration getSettingNames(){
		init();
		return settings.getStore().getSettingNames();
	}
	
	public static String getString(String name,String default_value){
		init();
		return settings.getStore().getString(name,default_value);
	}

	public static String[] getArray(String name,String default_value[]){
		init();
		return getArray(settings.getStore(),name,default_value);
	}
	
	public static String[] getArray(SettingsStore store,String name,String default_value[]){
		init();
		ArrayList<String> vs = new ArrayList<String>();
		boolean one_found =  (settings.getStore().doesNameExist(name+".0"));
		for (int i = 0;(one_found && settings.getStore().doesNameExist(name+"."+i)) || (!one_found && i < default_value.length);i++) {
			if (i < default_value.length)
				vs.add(settings.getStore().getString(name+"."+i,default_value[i]));
			else
				vs.add(settings.getStore().getString(name+"."+i,"no_value"));
		}
		return vs.toArray(new String[0]);
	}

	public static long getLong(String name,long default_value){
		init();
		return settings.getStore().getLong(name,default_value);
	}

	public static int getInt(String name,int default_value){
		init();
		return settings.getStore().getInt(name,default_value);
	}

	public static boolean getBoolean(String name,boolean default_value){
		init();
		return settings.getStore().getBoolean(name,default_value);
	}

	public static Date getTime(String name,Date default_value){
		init();
		return settings.getStore().getTime(name,default_value);
	}

	public static Date getDate(String name,Date default_value){
		init();
		return settings.getStore().getDate(name,default_value);
	}

	public static Date getDateTime(String name,Date default_value){
		init();
		return settings.getStore().getDateTime(name,default_value);
	}

	public static void setString(String name,String value){
		init();
		settings.getStore().setString(name,value);
	}

	public static void setLong(String name,long value){
		init();
		settings.getStore().setLong(name,value);
	}

	public static void setInt(String name,int value){
		init();
		settings.getStore().setInt(name,value);
	}

	public static void setBoolean(String name,boolean value){
		init();
		settings.getStore().setBoolean(name,value);
	}

	public static void setTime(String name,Date value){
		init();
		settings.getStore().setTime(name,value);
	}

	public static void setDate(String name,Date value){
		init();
		settings.getStore().setDate(name,value);
	}

	public static void setDateTime(String name,Date value){
		init();
		settings.getStore().setDateTime(name,value);
	}

	/**
	* This is mearly a sugested save point some stores will save automatically when you set a value or
	* a thier own descretion.
	*/
	public static void save(){
		init();
		settings.getStore().save();
	}
}

